/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dale2507.gates;

import dale2507.gates.commands.ConfiguratorCommand;
import dale2507.gates.commands.GateCommand;
import dale2507.gates.commands.ListGates;
import dale2507.gates.commands.SpawnGate;
import dale2507.gates.data.GateLoader;
import dale2507.gates.data.permissions.Permissions;
import dale2507.gates.exceptions.GateException;
import dale2507.gates.gate.Gate;
import dale2507.gates.misc.SignGateCreator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

/**
 *
 * @author Dale
 */
public class GatePlugin extends JavaPlugin {
    
    private static GatePlugin instance;
    
    public static GatePlugin getInstance() {
        return instance;
    }
    
    private GateDirectory directory = null;

    @Override
    public void onEnable() {
        GatePlugin.instance = this;
        
        Permissions.detectPermissions();
        
        // Register events
        PluginManager pm = this.getServer().getPluginManager();
        pm.registerEvents(new SignGateCreator(), this);
        
        // Setup commands
        this.getCommand("spawngate").setExecutor(new SpawnGate());
        this.getCommand("gatelist").setExecutor(new ListGates());
        this.getCommand("gate").setExecutor(new GateCommand());
        this.getCommand("gateconfigurator").setExecutor(new ConfiguratorCommand());
        
        GateLoader.getInstance().load();
        
        // Register spout if available
        if (Bukkit.getPluginManager().getPlugin("Spout") != null) {
            new SpoutDialSounds();
        }
        
        // Output status
        Logger.getLogger(GatePlugin.class.getName()).log(Level.INFO, getDescription().getFullName() + " is enabled.");
    }

    @Override
    public void onDisable() {
        for (Gate g : this.getGateDirectory().getGates()) {
            try {
                g.setEnabled(false);
            } catch (GateException ex) {
                Logger.getLogger(GatePlugin.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        directory = null;
        
        Logger.getLogger(GatePlugin.class.getName()).log(Level.INFO, getDescription().getFullName() + " is disabled.");
    }
    
    GateDirectory getGateDirectory() {
        if (directory == null) directory = new GateDirectory();
        return directory;
    }
    
}
